package onlineclass.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import onlineclass.entity.OMajor;
import onlineclass.entity.OResource;
import onlineclass.entity.OVideo;
import onlineclass.service.OMajorService;
import onlineclass.service.OResourceService;
import onlineclass.service.OVideoService;
import onlineclass.util.AjaxResult;
import onlineclass.util.StringUtil;
import onlineclass.util.AjaxPage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

import static onlineclass.util.AjaxResult.success;

public class OVideoController {

    public void search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"),1);
        String title = request.getParameter("title");
        AjaxPage types = OVideoService.INST.search(pageNo,10,title);
        response.setContentType("application/json;charset=utf-8");
        String str = JSON.toJSONStringWithDateFormat(types,"yyyy-MM-dd",
                SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(str);
    }
    public void add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String title = request.getParameter("title");
        Long courseid = Long.parseLong(request.getParameter("courseid"));
        String url = request.getParameter("url");
        String coverimg = request.getParameter("coverimg");
        Date createtime = StringUtil.parse(request.getParameter("createtime"));
        Date modifytime = StringUtil.parse(request.getParameter("modifytime"));
        int inuse = Integer.parseInt(request.getParameter("inuse"));
        OVideo v = new OVideo();
        v.setTitle(title);
        v.setCourseid(courseid);
        v.setUrl(url);
        v.setCoverimg(coverimg);
        v.setCreatetime(createtime);
        v.setModifytime(modifytime);
        v.setInuse(inuse);
        int add = OVideoService.INST.add(v);
        AjaxResult success;
        if (add>=1){
            success= AjaxResult.success(add);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONString(success);
        response.getWriter().print(jsonString);
    }
    //    接收修改对象的id
    public void get(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = StringUtil.toLong(request.getParameter("id"));
        OVideo oMajor = OVideoService.INST.get(id);
        AjaxResult success;
        if (oMajor!=null){
            success= AjaxResult.success(oMajor);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONStringWithDateFormat(success,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().println(jsonString);
    }

    public void update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = StringUtil.toLong(request.getParameter("id"));
        String title = request.getParameter("title");
        Long courseid = Long.parseLong(request.getParameter("courseid"));
        String url = request.getParameter("url");
        String coverimg = request.getParameter("coverimg");
        Date createtime = StringUtil.parse(request.getParameter("createtime"));
        Date modifytime = StringUtil.parse(request.getParameter("modifytime"));
        int inuse = Integer.parseInt(request.getParameter("inuse"));
        OVideo c = new OVideo();
        c.setId(id);
        c.setTitle(title);
        c.setCourseid(courseid);
        c.setUrl(url);
        c.setCoverimg(coverimg);
        c.setCreatetime(createtime);
        c.setModifytime(modifytime);
        c.setInuse(inuse);
        int update = OVideoService.INST.update(c);
        AjaxResult success;
        if (update>=1){
            success= AjaxResult.success(update);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONString(success);
        response.getWriter().print(jsonString);
    }
    public void del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        OVideo c = new OVideo();
        long id = StringUtil.toLong(request.getParameter("id"));
        c.setId(id);
        int del = OVideoService.INST.del(id);
        AjaxResult success;
        if (del>=1){
            success= AjaxResult.success(del);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONString(success);
        response.getWriter().print(jsonString);
    }
}
